<?php
/**
 * Ip 插件
 * @author 后盾向军 <houdunwangxj@gmail.com>
 */

class AdminController extends AddonController {

	public function __init() {
		$this->model = K('ip');
        $this->db = M('bangding');
	}

    public function index() {
    	
        $data = M('bangding')->all();
        $domain = array();
        foreach ($data as $key => $d) {
            $d['ip'] = $this->getIP($d['id']);
         
           $domain[] = $d;
        }
        $this->assign('domain',$domain);
        $this->display();
    }

    public function getIP($did)
    {
        $ips = M('ip')->where(array('did'=>$did))->select();
        $data = array();
        foreach ($ips as $key => $d) {
            $data[] = $d['ip'];
        }
        $data = implode("\r\n", $data);
        return $data;
    }

    /**
     * 添加屏蔽ip
     */
    public function add() {
    	if(IS_POST) {
 
    		$ips = trim(Q('ip'));
    		$ips = explode("\r\n", $ips);
            $did = Q('did');

    		foreach ($ips as $key => $value) {
                $ip = explode('.', $value);
                if(count($ip)==3)
                {
                    $style = "C";
                }else{
                    $style = "D";
                }
    			$this->model->addip($value,$style,$did);
    		}

    		go(U('index',array('g'=>'Addon'),''));

    	} else {

            $domain = M('bangding')->select();
            $this->assign('domain',$domain);

    		$this->display();
    	}
    	
    }

    public function del() {
    	$id = Q('id');
    	$where = array('id'=>array('in',$id));
    	$info = M('ip')->where($where)->del();
    	if($info) {
    		go(U('index',array('g'=>'Addon'),''));
    	}
    }

    public function sou() {
    	$sou = Q('sou_ip');
    	$where['ip'] = array('like',"%$sou%");

    	$data = M('ip')->where($where)->select();
        if(!$data) {
            $this->ajax("");
        }
            $html="";
        foreach ($data as $key => $d) {
            $html.="<tr>";
            if($d['status']==1){
                $status = "√";
            }else{
                $status = "x";
            }
            $html.='<td><input type="checkbox" name="id[]" value="'.$d['id'].'"/></td>
            <td>'.$d['id'].'</td>
            <td>'.$d['ip'].'</td>
            <td>'.$d['click'].'</td>
            <td>'.$d['type'].'</td>
            <td>'.$status.'</td>
            <td>'.date('Y-m-d H:i:s',$d['time']).'</td>
            <td><a href="'.U('del',array('g' => 'Addons','id'=>$d['id']),'').'">删除</a></td>';
            $html.="</tr>";
        }
        
    	$this->ajax($html);
    }


}